package model.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import dom.domain.Administrator;
import dom.domain.AdministratorDAO;

public class AdminService {
	private Administrator admin;
	private AdministratorDAO adminDao;
	private Map<String,String> map;
	
	
	public String login(String adminInfo)
	{
		map = new HashMap<String,String>();
		if(adminInfo==null)
		{
			map.put("flag", "false");
			map.put("Info", "信息为空");
			return JSONObject.fromObject(map).toString();
		}
		try
		{
			JSONObject jInfo = JSONObject.fromObject(adminInfo);
			if(!jInfo.containsKey("adminId")||!jInfo.containsKey("adminPsd"))
			{
				map.put("flag", "false");
				map.put("Info","用户名或密码为空");
				return JSONObject.fromObject(map).toString();
			}
			Administrator admin = adminDao.findById(jInfo.getString("adminId"));
			if(admin!=null)
			{
				if(admin.getAdminPsd().equals(jInfo.getString("adminPsd")))
				{
					map.put("flag", "true");
					map.put("Info", "登陆成功");
				}else
				{
					map.put("flag", "false");
					map.put("Info", "密码不正确");
				}
			}else
			{
				map.put("flag", "false");
				map.put("Info","用户民不存在");
			}
			
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("Info",e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String add(String adminInfo) 
	{
		map = new HashMap<String,String>();
		try
		{
		JSONObject jadmin = JSONObject.fromObject(adminInfo);
		if(!jadmin.containsKey("adminId"))
		{
			map.put("flag", "false");
			map.put("info", "adminId为空");
			return JSONObject.fromObject(map).toString();
		}
		Administrator temp = adminDao.findById(jadmin.getString("adminId"));
		if(temp!=null)
		{
			map.put("flag", "false");
			map.put("Info","adminId已存在");
			return JSONObject.fromObject(map).toString();
		}
		if(!jadmin.containsKey("adminPsd"))
		{
			map.put("flag", "false");
			map.put("info","adminPsd为空");
			return JSONObject.fromObject(map).toString();
		}
		if(!jadmin.containsKey("authority"))
		{
			map.put("flag","false");
			map.put("info", "authority为空");
			return JSONObject.fromObject(map).toString();
		}
		admin.setAdminId(jadmin.getString("adminId"));
		admin.setAdminPsd(jadmin.getString("adminPsd"));
		admin.setAuthority(jadmin.getString("authority"));
		adminDao.save(admin);
		map.put("flag", "true");
		map.put("info", "添加成功");
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	
	public String delete(String adminId)
	{
		map = new HashMap<String,String>();
		if(adminId==null)
		{
			map.put("flag","false");
			map.put("Info","adminId为空");
			return JSONObject.fromObject(map).toString();
		}
		Administrator tempAdmin = adminDao.findById(adminId);
		if(tempAdmin==null)
		{
			map.put("flag", "false");
			map.put("info", "adminId不存在");
		}
		else
		{
			adminDao.delete(tempAdmin);
			map.put("flag", "true");
			map.put("info", "删除成功");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String query(String adminId)
	{
		map = new HashMap<String,String>();
		if(adminId==null)
		{
			map.put("flag","false");
			map.put("Info","adminId为空");
			return JSONObject.fromObject(map).toString();
		}
		admin = adminDao.findById(adminId);
		if(admin==null)
		{
			map.put("flag", "fasle");
			map.put("info","adminId不存在");
		}
		else
		{
			try
			{
			map.put("adminInfo", admin.toString());
			map.put("flag", "true");
			map.put("info", "查询成功");
			}catch(JSONException e)
			{
				map.put("flag","false");
				map.put("info", e.getMessage());
			}
		}
		return JSONObject.fromObject(map).toString();
	}
	public String modify(String adminInfo)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jadmin = JSONObject.fromObject(adminInfo);
			if(!jadmin.containsKey("adminId"))
			{
				map.put("flag", "false");
				map.put("info", "adminId为空");
				return JSONObject.fromObject(map).toString();
			}
			admin = adminDao.findById(jadmin.getString("adminId"));
			if(admin==null)
			{
				map.put("flag", "false");
				map.put("info", "adminId不存在");
				return JSONObject.fromObject(map).toString();
			}
			if(jadmin.containsKey("adminPsd"))
			{
				admin.setAdminPsd(jadmin.getString("adminPsd"));
			}
			if(jadmin.containsKey("authority"))
			{
				admin.setAuthority(jadmin.getString("authority"));
			}
			adminDao.attachDirty(admin);
			map.put("flag", "true");
			map.put("info", "修改成功");
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	
	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}
	public void setAdminDao(AdministratorDAO adminDao) {
		this.adminDao = adminDao;
	}
}
